/*
 RLRecyclableItemButtonController.h
 
 Date Created: February 19, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "RLRecyclableItemButton.h"

#import "RLRecyclableType.h"
#import "RLStyleFormatter.h"


@implementation RLRecyclableItemButton

@synthesize itemIconView;
@synthesize nameLabel;
@synthesize quantityLabel;
@synthesize item;

- (RLRecyclableItemButton *)initWithItem:(RLRecyclableItem *)itemArg {
  self=[super initWithFrame:CGRectMake(0,0,120,100)];
  if( self) {
		// Configure button
		//self.backgroundColor=UIColor.brownColor;
		self.contentMode = UIViewContentModeScaleToFill;
		
    // Configure the background image
    backgroundView=[[UIImageView alloc] initWithFrame:self.frame];
    CGRect backgroundViewFrame=CGRectMake(0, 0, 0, 0);
		backgroundViewFrame.size=self.frame.size; //icon.size;
    backgroundView.frame=backgroundViewFrame;
    backgroundView.contentMode = UIViewContentModeScaleAspectFit;
    backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                      UIViewAutoresizingFlexibleWidth | 
                                      UIViewAutoresizingFlexibleLeftMargin | 
                                      UIViewAutoresizingFlexibleRightMargin | 
                                      UIViewAutoresizingFlexibleTopMargin |
                                      UIViewAutoresizingFlexibleBottomMargin;
    backgroundView.image=[UIImage imageNamed:@"RLItemBackground.png"];
    
		// Configure icon
		itemIconView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 70)];
		itemIconView.contentMode = UIViewContentModeScaleAspectFit;
    itemIconView.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                    UIViewAutoresizingFlexibleWidth | 
                                    UIViewAutoresizingFlexibleLeftMargin | 
                                    UIViewAutoresizingFlexibleRightMargin | 
                                    UIViewAutoresizingFlexibleTopMargin |
                                    UIViewAutoresizingFlexibleBottomMargin;
    
		// Configure name label
    nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(5,70,95,40)];
    nameLabel.textAlignment=UITextAlignmentCenter;
    nameLabel.numberOfLines=2;
    UIFont *nameFont=[UIFont systemFontOfSize:16];
    nameLabel.font=nameFont;
    nameLabel.backgroundColor=UIColor.clearColor;
    nameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                 UIViewAutoresizingFlexibleWidth | 
                                 UIViewAutoresizingFlexibleLeftMargin | 
                                 UIViewAutoresizingFlexibleRightMargin | 
                                 UIViewAutoresizingFlexibleTopMargin |
                                 UIViewAutoresizingFlexibleBottomMargin;
    
	  
		// Configure quantity label
    quantityLabel=[[UILabel alloc] initWithFrame:CGRectMake(50,20,60,60)];
    quantityLabel.textAlignment=UITextAlignmentCenter;
    UIFont *quantityFont=[UIFont boldSystemFontOfSize:36];
    quantityLabel.font=quantityFont;
    quantityLabel.backgroundColor=UIColor.clearColor;
    quantityLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | 
                                     UIViewAutoresizingFlexibleWidth | 
                                     UIViewAutoresizingFlexibleLeftMargin | 
                                     UIViewAutoresizingFlexibleRightMargin | 
                                     UIViewAutoresizingFlexibleTopMargin |
                                     UIViewAutoresizingFlexibleBottomMargin;
    
    [self addSubview:backgroundView];
    [self addSubview:itemIconView];
    [self addSubview:nameLabel];
    [self addSubview:quantityLabel];
    
    self.item=itemArg;
  }
  return self;
}

- (void)setItem:(RLRecyclableItem *)newItem {
  if(item) {
    [item removeObserver:self forKeyPath:@"quantity"];
    [item release];
  }
  item=newItem;
  [item retain];

  [item addObserver:self 
         forKeyPath:@"quantity" 
            options:0
            context:nil];
  
  UIImage *icon=[UIImage imageNamed:item.typeDescription.icon];
  itemIconView.image=icon;
  
  self.nameLabel.text=[RLStyleFormatter titleForType:[RLRecyclableType typeForUniqueName:self.item.type]];
  self.quantityLabel.text=[NSString stringWithFormat:@"%d",self.item.quantity];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
  if([keyPath isEqualToString:@"quantity"]) {
    self.quantityLabel.text=[NSString stringWithFormat:@"%d",self.item.quantity];
  }
}

- (void)dealloc {
  [item release];
  
  [backgroundView release];
  [itemIconView release];
  [quantityLabel release];
  [nameLabel release];
  
  [super dealloc];
}


@end
